﻿using EYangFrame.DataAccess.Global;
using EYangFramework.Common.Timer;
using EYangFramework.Domain.Entitys;
using EYangFramework.Domain.Repositorys;
using Ninject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Menu = EYangFramework.Domain.Entitys.ValueObjects.Menu;

namespace EYangFramework.CommonForm
{
    public partial class frm_BasicForm : Form
    {
        [Inject]
        public IInstanceTimer InstanceTimer { get; set; }
        [Inject]
        public ISingtonTimer SingtonTimer { get; set; }
        [Inject]
        public IRepository<Menu> RepositoryMenu { get; set; }

        public bool AllowSingtonTimer = true;
        public bool AllowInstanceTimer = false;
        public int InstancePeriod = 5;
        public dynamic TempTool { get; set; }
        public frm_BasicForm()
        {
            if(!DesignMode)
            {
                this.TempTool = GlobalInfo.TempTool;
                InitializeComponent();
            }
        }
        public virtual MenuStrip GetMenuMain()
        {
            return null;
        }
        public virtual void ExecuteSingtonTimer(object sender, EventArgs args)
        { }
        public virtual void ExecuteInstanceTimer(object sender, EventArgs args)
        { }

        public void AddSingtonTimer()
        {
            if(AllowSingtonTimer)
            {
                SingtonTimer.Event += ExecuteSingtonTimer;
            }
        }
        public void AddInstanceTimer()
        {
            if (AllowInstanceTimer)
            {
                InstanceTimer.Event += ExecuteInstanceTimer;
            }
        }

        public void RemoveSingtonTimer()
        {
            if (AllowSingtonTimer)
            {
                SingtonTimer.Event -= ExecuteSingtonTimer;
            }
        }
        public void RemoveInstanceTimer()
        {
            if (AllowInstanceTimer)
            {
                InstanceTimer.Event -= ExecuteInstanceTimer;
            }
        }

        private void frm_BasicForm_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                AddSingtonTimer();
                AddInstanceTimer();

                //设置菜单权限
                SetMenusAuth(this, $"{this.GetType().Assembly.GetName().Name}.{this.GetType().Name}");

                toolStatusUser.Text = string.Format($"欢迎您:{GlobalInfo.CurrentUser.USERNAME}!");
            }
        }

        private void frm_BasicForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            InstanceTimer.Stop();
            RemoveInstanceTimer();
            RemoveSingtonTimer();
            if (this.Tag == null)
            {
                return;
            }
            (this.Tag as Form).Show();
        }

        private void SetMenusAuth(frm_BasicForm currentForm, string currentType)
        {
            MenuStrip menuStrip = currentForm.GetMenuMain();
            if (menuStrip == null || menuStrip.Items.Count <= 0)
            {
                return;
            }
            List<Menu> menus = RepositoryMenu.GetAllList(t => t.FORMTYPE == currentType);
            //如果当前用户是管理员或者没有设置权限,则不对菜单作权限控制
            if (GlobalInfo.CurrentUser.ISADMIN || menus == null)
            {
                return;
            }
            else
            {
                //用户拥有的权限应该包括自身拥有的权限以及其角色拥有的权限
                menus = GlobalInfo.CurrentUser.MENUS.Union(GlobalInfo.CurrentUser.ROLE.MENUS).ToList();
                if (menus == null || menus.Count <= 0)
                {
                    return;
                }
            }
            List<string> menuNames = menus.Where(t => t.FORMTYPE == $"{this.GetType().Assembly.GetName().Name}.{this.GetType().Name}").Select(t => t.MENUNAME).Distinct().ToList();
            SetElementAuth(menuStrip as ToolStrip, menuNames);//为子节点授权
        }
        private void SetElementAuth(ToolStrip toolStrip, List<string> menuNames)
        {
            foreach (ToolStripMenuItem item in toolStrip.Items)
            {
                if (menuNames.Where(t => t == item.Name).Count() > 0)
                {
                    //只有当前节点拥有权限,才对其子节点进行权限设置
                    item.Visible = true;
                    if (item.Owner.Items.Count > 0)
                    {
                        SetElementDropDown(item, menuNames);
                    }
                }
                else
                {
                    item.Visible = false;
                }
            }
        }
        private void SetElementDropDown(ToolStripDropDownItem dropDownItem, List<string> menuNames)
        {
            foreach (ToolStripMenuItem item in dropDownItem.DropDownItems)
            {
                if (menuNames.Where(t => t == item.Name).Count() > 0)
                {
                    //只有当前节点拥有权限,才对其子节点进行权限设置
                    item.Visible = true;
                    if (item.Owner.Items.Count > 0)
                    {
                        SetElementDropDown(item, menuNames);
                    }
                }
                else
                {
                    item.Visible = false;
                }
            }
        }
        public void SetTabVisible(TabPage tp_Current,bool visisble)
        {
            foreach (TabPage item in tab_Main.TabPages)
            {
                item.Parent = null;
            }
            if (visisble)
            {
                tp_Current.Parent = tab_Main;
            }
            else
            {
                tp_Current.Parent = null;
                tp_Main.Parent = tab_Main;
            }
        }

        private void toolButton_ButtonClick(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定需要退出程序吗?", "警告!", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Application.Exit();
            }
        }
    }
}
